package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.base.BaseConfig;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogMessage;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logMessage.LogMessageConfirmReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logMessage.LogMessageCountResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logMessage.LogMessageSelectPageReqVo;
import com.zmj.sy.mom.srv.aps.mapper.BaseConfigMapper;
import com.zmj.sy.mom.srv.aps.mapper.LogMessageMapper;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import com.zmj.sy.mom.srv.aps.utils.SySecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@RequiredArgsConstructor
@Service
public class LogMessageService {

    private final LogMessageMapper logMessageMapper;
    private final BaseConfigMapper baseConfigMapper;

    public BasePageResVo<LogMessage> messageSelectDetail(LogMessageSelectPageReqVo reqVo) {
        QueryWrapper<LogMessage> qw = Wrappers.query();
        if (StringUtils.hasText(reqVo.getConfUser())) {
            qw.eq("conf_user", reqVo.getConfUser());
        }
        if (!ObjectUtils.isEmpty(reqVo.getConfState())) {
            qw.eq("conf_state", reqVo.getConfState());
        }
        qw.orderByAsc("conf_state");
        Page<LogMessage> pageVo = logMessageMapper.selectPage(reqVo.toPage(), qw);
        return BasePageResVo.of(pageVo, LogMessage.class);
    }

    public BaseListResVo<LogMessage> messageSelect() {
        QueryWrapper<LogMessage> qw = Wrappers.query();
        qw.eq("conf_state", 0);
        qw.orderByDesc("create_date_time");
        qw.last("limit 5");
        List<LogMessage> logMessageList = logMessageMapper.selectList(qw);
        return BaseListResVo.of(logMessageList, LogMessage.class);
    }

    public void confirm(LogMessageConfirmReqVo reqVo) {
        LogMessage log = logMessageMapper.lambdaQuery().eq(LogMessage::getId, reqVo.getId()).eq(LogMessage::getConfState, 0).one();
        BaseConfig config = baseConfigMapper.lambdaQuery().eq(BaseConfig::getLabelType, "nest").eq(BaseConfig::getLabelCode, "nestPartyUser").one();

        if (log == null) {
            throw SyExceptionUtils.e("未找到需确认的信息:{}", reqVo.getId());
        }
        String user = SySecurityUtils.getUserCode();
        if (!config.getValueData().contains(user)) {
            throw SyExceptionUtils.e("当前用户不具备确认权限:{}", user);
        }
        log.setConfUser(user);
        log.setConfState(1);
        if (StringUtils.hasText(reqVo.getDealMes())) {
            log.setDealMes(reqVo.getDealMes());
        }
        logMessageMapper.updateById(log);
    }

    public LogMessageCountResVo messageCount() {
      /*  QueryWrapper<LogMessage> qw = Wrappers.query();
        qw.eq("conf_state", 0);
        Integer i = logMessageMapper.selectCount(qw);
          vo.setCount(i);*/
        LogMessageCountResVo vo = new LogMessageCountResVo();
        return vo;
    }

}
